package com.dongdong.controller;

import com.dongdong.model.TbGoods;
import com.dongdong.model.TbSeller;
import com.dongdong.result.PageBean;
import com.dongdong.result.PageResult;
import com.dongdong.result.Result;
import com.dongdong.result.ResultConstant;
import com.dongdong.service.GoodsService;

import com.dongdong.utils.JwtUtil;
import com.dongdong.vo.GoodsVO;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("tbgoods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private JwtUtil jwtUtil;

    //增加
    @PostMapping("add")
    public Result add(@RequestHeader String token, @RequestBody GoodsVO goodsVO){
        Claims claims = jwtUtil.parseJwt(token);  //获取运营商
        String sellerId = claims.getId();
        goodsVO.setSellerId(sellerId);

        goodsService.insert(goodsVO);
        return  Result.OK(); //4：操作成功
    }

    @PostMapping("/findAllPage")
    public Result findAllPage(@RequestBody PageBean<TbGoods> pageBean){
        PageResult result = goodsService.findAllPage(pageBean);
        return Result.OK().setData(result);
    }

    //详情
    @GetMapping("/selectById")
    public TbGoods selectById(@RequestParam String id){
        return goodsService.selectById(id);
    }

    //审核
    @PostMapping("/updateStatus")
    public Result updateStatus(@RequestParam String auditStatus,@RequestParam Long id){
        goodsService.updateStatus(auditStatus,id);
        return Result.OK();
    }
    @GetMapping("/selectGoodsAndDesc")
    public Result selectGoodsAndDesc(@RequestParam Long goodId){
        GoodsVO goodVO = goodsService.selectGoodsAndDesc(goodId);
        return Result.OK().setData(goodVO);
    }




}
